/* ***********************************************************
* A short word on how to use this automagically generated file.
* We're often asked in the ignite gitter channel how to connect
* to a to a third party api, so we thought we'd demonstrate - but
* you should know you can use sagas for other flow control too.
*
* Other points:
*  - You'll need to add this saga to sagas/index.js
*  - This template uses the api declared in sagas/index.js, so
*    you'll need to define a constant in that file.
*************************************************************/

import { call, put } from 'redux-saga/effects'
import AddressActions from '../Redux/AddressRedux'

export function * getAllAddress (api, action) {
  const { token } = action
  const response = yield call(api.getAllAddress, token)
  if (response.ok) {
    yield put(AddressActions.getAllSuccess(response.data))
  } else {
    yield put(AddressActions.getAllFailure())
  }
}

export function * createAddress (api, action) {
  const { token, data } = action
  const response = yield call(api.createAddress, token, data)
  if (response.ok && response.data.code === 1) {
    yield put(AddressActions.createSuccess(response.data.data))
  } else {
    yield put(AddressActions.createFailure())
  }
}

export function * updateAddress (api, action) {
  const { token, data } = action
  const response = yield call(api.updateAddress, token, data)
  if (response.ok && response.data.code === 1) {
    yield put(AddressActions.updateSuccess())
  } else {
    yield put(AddressActions.updateFailure())
  }
}

export function * deleteAddress (api, action) {
  const { token, id } = action
  const response = yield call(api.deleteAddress, token, id)
  if (response.ok && response.data.code === 1) {
    yield put(AddressActions.deleteSuccess())
  } else {
    yield put(AddressActions.deleteFailure())
  }
}
